<?php
/**
 * Created by PhpStorm.
 * User: ownfi
 * Date: 2017-03-20
 * Time: 14:17
 */

namespace App\Services\Impl;


use App\Common\Constant\CookieKey;
use App\Services\ICookieService;
use Cookie;

class CookieServiceImpl implements ICookieService
{

    private function getter($key){
        return request()->cookie($key);
    }

    private function setter($key,$value,$minutes){
        Cookie::queue($key,$value,$minutes);
        return Cookie::hasQueued($key);
    }

    public function verifiedPhone($phoneNum = null, $minutes = 10)
    {
        $cookieKey = CookieKey::verifiedPhoneKey();
        if ($phoneNum === null){
            //getter
            if (isMock()){
                return 15051877991;
            }
            return $this->getter($cookieKey);
        }else{
            //setter
            return $this->setter($cookieKey,$phoneNum,$minutes);
        }
    }

    public function currentUserid($userid = null, $minutes = 30)
    {
        $cookieKey = CookieKey::currentUseridKey();
        if ($userid === null){
            //getter
            return $this->getter($cookieKey);
        }else{
            //setter
            return $this->setter($cookieKey,$userid,$minutes);
        }
    }

    public function currentWeixinUserid($weixinUserid = null, $minutes = 60)
    {
        $cookieKey = CookieKey::currentWeixinUseridKey();
        if ($weixinUserid === null){
            //getter
            return $this->getter($cookieKey);
        }else{
            //setter
            return $this->setter($cookieKey,$weixinUserid,$minutes);
        }
    }

}